package py.una.pol.pweb.compraventa;

import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import py.una.pol.pweb.compraventa.modelo.Cliente;



@ManagedBean
@SessionScoped
public class ClienteBean implements Serializable{
	
	public ClienteBean(){}

	private Cliente cliente = new Cliente ();
        private Cliente clienteSelected=new Cliente();

        public Cliente getClienteSelected() {
            return clienteSelected;
        }

        public void setClienteSelected(Cliente clienteSelected) {
            this.clienteSelected = clienteSelected;
        }
	
	
	public Cliente getCliente() {
		return cliente;
	}


	public void setCliente(Cliente cliente) {
		this.cliente = cliente;
	}


	public void guardarCliente() {
            
            /*FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage("somekey", facesMessage1);

             */
            FacesContext context = FacesContext.getCurrentInstance();
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("CompraVentaPU");
            EntityManager em = emf.createEntityManager();
            try {

                em.getTransaction().begin();
                cliente = new Cliente();
                cliente.setNombre(clienteSelected.getNombre());
                cliente.setDireccion(clienteSelected.getDireccion());
                cliente.setTelefono(clienteSelected.getTelefono());
                cliente.setSaldo(0.0);
                em.persist(cliente);
                em.getTransaction().commit();



                clienteSelected = new Cliente();
                //context.addMessage("addCliente", new FacesMessage(FacesMessage.SEVERITY_INFO, "El Cliente fue agregado con exito!", null));
                context.addMessage(null, new FacesMessage("Exito!", cliente.getNombre() + ", agregado correctamete."));

            } catch (Exception e) {

                e.printStackTrace();
            } finally {
                em.close();
            }
	}

    @SuppressWarnings("unchecked")
    public List<Cliente> getAllClientes() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CompraVentaPU");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
 
        Query allClientesQuery = em.createQuery("select d from Cliente d");
 
        List<Cliente> allClientes = allClientesQuery.getResultList();
 
        tx.commit();
        em.close();
        cliente=new Cliente();
        return allClientes;
    }
    
    
    public void modificarCliente(){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CompraVentaPU");
        EntityManager em = emf.createEntityManager();
        FacesContext context = FacesContext.getCurrentInstance();
        try {
            em.getTransaction().begin();
            Cliente userx = em.find(Cliente.class, clienteSelected.getIdcliente());
            userx.setNombre(clienteSelected.getNombre());
            userx.setDireccion(clienteSelected.getDireccion());
            userx.setTelefono(clienteSelected.getTelefono());
            em.getTransaction().commit();
            clienteSelected=new Cliente();
            context.addMessage(null, new FacesMessage("Modificado exitosamente!", cliente.getNombre()+", agregado correctamete."));
        } finally {
            em.close();
            
        }
        
    
    }
    
    public String clienteSelectedNombreChanged(){
            
        return clienteSelected.getNombre();
    
    }
    public void limpiarForm(){
        clienteSelected=new Cliente();
    
    }
    
    public void agregarExito(){
    
        FacesMessage msg = new FacesMessage("Cliente Agregado", cliente.getNombre());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    
    }
    public void eliminarCliente(){
        FacesContext context = FacesContext.getCurrentInstance();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("CompraVentaPU");
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            Cliente userx = em.find(Cliente.class, clienteSelected.getIdcliente());
            em.remove(userx);
            em.getTransaction().commit();
            limpiarForm();
            context.addMessage(null, new FacesMessage("Eliminado exitosamente!", cliente.getNombre()+", agregado correctamete."));
            
        } finally {
            em.close();
            
        }
    
    }
}
